//
//  PGEventListView.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-18.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGEventListView.h"
#import "EpointDateUtils.h"
#import "UIView+Addtions.h"
#import "PGEventItem.h"

#define KeyEarly @"凌晨"
#define KeyMorning @"上午"
#define KeyNoon @"中午"
#define KeyAfternoon @"下午"
#define KeyEvening @"晚上"

#define TopPadding 30
#define LeftPadding 50
#define CellHeight 50
#define CellMargin 5

@interface PGEventListView () <PGEventItemDelegate>

@property (nonatomic, strong) NSMutableDictionary *dataDic;

@end

@implementation PGEventListView {
    CGFloat _cellTop;
    CGFloat _cellWidth;
    CGFloat _cellHeight;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _cellWidth = frame.size.width;
        self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_contentView];
    }
    return self;
}

#pragma mark - addSubview

- (void)reloadData {
    [self removeAllSubViews];
    [self addSubview];
    [self setNeedsDisplay];
}

- (void)removeAllSubViews {
    for (UIView *v in _contentView.subviews) {
        [v removeFromSuperview];
    }
}

- (void)addSubview {
    _cellTop = TopPadding;
    [self addHorizontalLine];
    NSArray *earlyArray = [_dataDic objectForKey:KeyEarly];
    [self addCellAsArray:earlyArray isEvening:NO];
    NSArray *morningArray = [_dataDic objectForKey:KeyMorning];
    [self addCellAsArray:morningArray isEvening:NO];
    NSArray *noonArray = [_dataDic objectForKey:KeyNoon];
    [self addCellAsArray:noonArray isEvening:NO];
    NSArray *afternoonArray = [_dataDic objectForKey:KeyAfternoon];
    [self addCellAsArray:afternoonArray isEvening:NO];
    NSArray *eveningArray = [_dataDic objectForKey:KeyEvening];
    [self addCellAsArray:eveningArray isEvening:YES];
    
    [self addVerticalLine];
    
    UILabel *earlyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TopPadding, LeftPadding, 21)];
    earlyLabel.text = KeyEarly;
    earlyLabel.centerY = TopPadding;
    earlyLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:earlyLabel];
    
    UILabel *morningLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TopPadding, LeftPadding, 21)];
    morningLabel.text = KeyMorning;
    morningLabel.centerY = earlyLabel.centerY + (earlyArray.count>1?earlyArray.count:1)*CellHeight;
    morningLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:morningLabel];
    
    UILabel *noonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TopPadding, LeftPadding, 21)];
    noonLabel.text = KeyNoon;
    noonLabel.centerY = morningLabel.centerY + (morningArray.count>1?morningArray.count:1)*CellHeight;
    noonLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:noonLabel];
    
    UILabel *afternoonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TopPadding, LeftPadding, 21)];
    afternoonLabel.text = KeyAfternoon;
    afternoonLabel.centerY = noonLabel.centerY + (noonArray.count>1?noonArray.count:1)*CellHeight;
    afternoonLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:afternoonLabel];
    
    UILabel *eveningLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TopPadding, LeftPadding, 21)];
    eveningLabel.text = KeyEvening;
    eveningLabel.centerY = afternoonLabel.centerY + (afternoonArray.count>1?afternoonArray.count:1)*CellHeight;
    eveningLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:eveningLabel];
}

- (void)addCellAsArray:(NSArray *)array isEvening:(BOOL)animated {
    if (array.count == 0) {
        _cellTop += CellHeight;
        if (animated == NO) {
            [self addHorizontalLine];
        }
    } else {
        for (ScheduleInfo *scheduleInfo in array) {
            [self addCell:scheduleInfo];
        }
    }
}

- (void)addHorizontalLine {
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(LeftPadding, _cellTop, _cellWidth-LeftPadding, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    line.alpha = 0.8;
    [_contentView addSubview:line];
}

- (void)addVerticalLine {

    _contentView.contentSize = CGSizeMake(_contentView.width, _contentView.height);
    if (_contentView.height < _cellTop) {
        _contentView.contentSize = CGSizeMake(_contentView.width, _cellTop + TopPadding);
    }
    
    CGFloat lineHeight = _contentView.contentSize.height - TopPadding;
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(LeftPadding, TopPadding, 1, lineHeight)];
    line.alpha = 0.8;
    line.backgroundColor = [UIColor lightGrayColor];
    [_contentView addSubview:line];
}

- (void)addCell:(ScheduleInfo *)scheduleInfo {
    PGEventItem *cell = [[PGEventItem alloc] initWithFrame:CGRectMake(LeftPadding+CellMargin, _cellTop+CellMargin, _cellWidth - CellMargin*2-LeftPadding, CellHeight - CellMargin*2)];
    cell.scheduleInfo = scheduleInfo;
    cell.delegate = self;
    [_contentView addSubview:cell];
//    cell.backgroundColor = [UIColor yellowColor];
    _cellTop += CellHeight;
    [self addHorizontalLine];
}

#pragma mark - property

- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    
    if (self.dataDic == nil) {
        self.dataDic = [[NSMutableDictionary alloc] initWithCapacity:5];
    } else {
        [_dataDic removeAllObjects];
    }
    
    [self fillDicWithNullArray];
    [self fillDicWithData];
}

- (void)fillDicWithNullArray {
    [_dataDic setObject:[NSMutableArray array] forKey:KeyEarly];
    [_dataDic setObject:[NSMutableArray array] forKey:KeyMorning];
    [_dataDic setObject:[NSMutableArray array] forKey:KeyNoon];
    [_dataDic setObject:[NSMutableArray array] forKey:KeyAfternoon];
    [_dataDic setObject:[NSMutableArray array] forKey:KeyEvening];
}

- (void)fillDicWithData {
    for (ScheduleInfo *scheduleInfo in _dataArray) {
        [self addScheduleInfoToRightArray:scheduleInfo];
    }
}

- (void)addScheduleInfoToRightArray:(ScheduleInfo *)scheduleInfo {
    NSString *fromDateStr = [EpointDateUtils convertStr2FromStr:scheduleInfo.FromDateTime oldFormat:@"yyyy-MM-dd HH:mm" withNewFormat:@"HHmm"];
    if ([fromDateStr compare:@"0100"] == NSOrderedAscending) {
        [self addToArrayWith:KeyEarly scheduleInfo:scheduleInfo];
        return;
    }
    
    if ([fromDateStr compare:@"1200"] == NSOrderedAscending) {
        [self addToArrayWith:KeyMorning scheduleInfo:scheduleInfo];
        return;
    }
    
    if ([fromDateStr compare:@"1400"] == NSOrderedAscending) {
        [self addToArrayWith:KeyNoon scheduleInfo:scheduleInfo];
        return;
    }
    
    if ([fromDateStr compare:@"1800"] == NSOrderedAscending) {
        [self addToArrayWith:KeyAfternoon scheduleInfo:scheduleInfo];
        return;
    }
    [self addToArrayWith:KeyEvening scheduleInfo:scheduleInfo];
}

- (void)addToArrayWith:(NSString *)keyName scheduleInfo:(ScheduleInfo *)scheduleInfo{
    NSMutableArray *array = [_dataDic valueForKey:keyName];
    [array addObject:scheduleInfo];
}

#pragma mark - PGEventItemDelegate

- (void)deleteItem:(PGEventItem *)item {
    [_dataArray removeObject:item.scheduleInfo];
    self.dataArray = _dataArray;
    [self reloadData];
    if (_delegate && [_delegate respondsToSelector:@selector(delListItem:)]) {
        [_delegate delListItem:item.scheduleInfo];
    }
}

- (void)editItem:(PGEventItem *)item {
    if (_delegate && [_delegate respondsToSelector:@selector(editListItem:)]) {
        [_delegate editListItem:item.scheduleInfo];
    }
}

@end